package com.ixuea.courses.mycloudmusicapisp.config;

import com.ixuea.courses.mycloudmusicapisp.interceptor.AuthorizationInterceptor;
import com.ixuea.courses.mycloudmusicapisp.resolver.CurrentUserArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * WebMvc相关配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 用户认证拦截器
     */
    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    /**
     * 当前用户参数解析器
     */
    @Autowired
    private CurrentUserArgumentResolver currentUserArgumentResolver;

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //多个拦截器组成一个拦截器链
        //addPathPatterns用于添加拦截规则
        //excludePathPatterns用于排除拦截
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns("/**");
    }


    /**
     * 添加参数解析器
     *
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(currentUserArgumentResolver);
    }

}
